/*
 * tetrixpiece.cpp - The individual tetrix pieces are defined here
 * Copyright (C) 2006-2007  Mithin Kumar L
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this application; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "../include/tetrixpiece.h"

#include <stdlib.h>
#include <time.h>

#include <QtGlobal>

TetrixPiece::TetrixPiece()
{
	rStatus = 0;
	setShape(NoShape);

	srand(time(NULL));
}

void TetrixPiece::setRandomShape()
{
    setShape(TetrixShape((rand() % 8)-1));
	//setShape(LineShape);
}

void TetrixPiece::setShape(TetrixShape shape)
{
	for (int i = 0; i < 4 ; i++) {
        for (int j = 0; j < 2; ++j)
            coords[i][j] = coordsTable[(int)shape+1][rStatus][i][j];
    }
    pieceShape = shape;
}

int TetrixPiece::minX() const
{
    int min = coords[0][0];
    for (int i = 1; i < 4; ++i)
        min = qMin(min, coords[i][0]);
    return min;
}

int TetrixPiece::maxX() const
{
    int max = coords[0][0];
    for (int i = 1; i < 4; ++i)
        max = qMax(max, coords[i][0]);
    return max;
}

int TetrixPiece::minY() const
{
    int min = coords[0][1];
    for (int i = 1; i < 4; ++i)
        min = qMin(min, coords[i][1]);
    return min;
}

int TetrixPiece::maxY() const
{
    int max = coords[0][1];
    for (int i = 1; i < 4; ++i)
        max = qMax(max, coords[i][1]);
    return max;
}

/*void TetrixPiece::rotateLeft() const
{
    if (pieceShape == SquareShape)
        return *this;

    TetrixPiece result;
    result.pieceShape = pieceShape;
    for (int i = 0; i < 4; ++i) {
        result.setX(i, y(i));
        result.setY(i, -x(i));
    }
    return result;
}

void TetrixPiece::rotateRight() const
{
	if (pieceShape == SquareShape)
        return *this;

    TetrixPiece result;
    result.pieceShape = pieceShape;
    for (int i = 0; i < 4; ++i) {
        result.setX(i, -y(i));
        result.setY(i, x(i));
    }
    return result;
}*/

void TetrixPiece::rotate(const int direction,bool idle)
{
	rStatus += direction;
	if(rStatus > 3)
		rStatus = 0;
	if(rStatus < 0)
		rStatus = 3;

	for (int i = 0; i < 4 ; i++) {
        for (int j = 0; j < 2; ++j)
            coords[i][j] = coordsTable[(int)pieceShape+1][rStatus][i][j];
    }
	if(idle)
		return;

}

TetrixShape TetrixPiece::shape() const 
{ 
	return pieceShape; 
}

int TetrixPiece::x(int index) const 
{ 
	return coords[index][0];
}

int TetrixPiece::y(int index) const 
{ 
	return coords[index][1]; 
}